package com.ddyx.service.base;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public abstract class BaseSaveFaceNodifyService implements Runnable {
	protected Thread currentThread;
//	public abstract String getListenQueueName();
	public abstract void doMessage(MapMessage mapMessage);
	public abstract void doMessage(TextMessage textMessage);
	public abstract String getQueueName();
	public static final String url = "tcp://120.27.20.74:61616";
	public static Properties faceos;
	public static boolean isRunning=false;
	public static int _interval=100;//获取消息的时间见过
	public static int _sleep=500;//线程睡眠时间
	@Override
	public void run() {
        Connection connection = null;  
        Session session = null;
		try
		{
			// Create a ConnectionFactory
	        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
	
	        // Create a Connection
	        connection = connectionFactory.createConnection();
	        connection.start();
	
	        // Create a Session
	        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	
	        // Create the destination (Topic or Queue)
	        String queueName = getQueueName();
	        if(queueName==null || "".equals(queueName))return;
	        Destination destination = session.createQueue(queueName);
	        
	        // Create a MessageConsumer from the Session to the Topic or Queue
	        MessageConsumer consumer = session.createConsumer(destination);
	
	        // Wait for a message, loop forever
	        boolean loop = true;
	        while(loop)
	        {
	        	Message message = consumer.receive(_interval);
	        	if (message == null)
	        	{
	        		continue;
	        	}
		        String text = null;
		
		        if (message instanceof TextMessage) {
		            TextMessage textMessage = (TextMessage) message;
		            text = textMessage.getText();
		            this.doMessage((TextMessage)message);
		        } 
		        else if (message instanceof MapMessage)
		        {
		        	this.doMessage((MapMessage)message);
		        }
		        else {
		            text = message.toString();
		        }
		        currentThread.sleep(_sleep);
	        }
	
	        consumer.close();
	        session.close();
	        connection.close();
		} catch (JMSException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		finally
		{
			try{  
                // close session and connection  
                if (session != null){  
                    session.close();  
                }  
                if (connection != null){  
                    connection.close();  
                }  
            }catch (Exception e){  
                e.printStackTrace();  
            }  			
		}
		
	}
	public void startListen(int interval,int sleep)
	{
		_interval=interval;
		_sleep=sleep;
		startListen();
	}
	//开始侦听消息
	public void startListen()
	{
		if(isRunning==false) {
			currentThread = new Thread(this);
			currentThread.start();
			isRunning=true;
		}
	}
	public boolean isRunning() {
		
		return isRunning;
	}
	public void stopListen()
	{
		currentThread.stop();
		isRunning=false;
	}
}
